package com.skycong.hello77.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * json  java 格式转换工具
 *
 * @author CreateBy RMC 2017/12/21
 */
public final class JsonJavaConvertUtil {

    private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();

    public static ObjectMapper getInstance() {
        return OBJECTMAPPER;
    }

    /**
     * 转换jsonp格式数据
     *
     * @param jsonData 需要转换的json数据
     * @param callback 回调方法名
     * @return jsonp格式数据
     */
    // public static Object json2Jsonp(Object jsonData, String callback) {
    //     MappingJacksonValue jacksonValue = new MappingJacksonValue(jsonData);
    //     jacksonValue.setJsonpFunction(callback);
    //     return jacksonValue;
    // }

    /**
     * javaBean,list,array,map convert to json string
     *
     * @param object it can be javaBean、list、array、map
     * @return json string
     * @throws JsonProcessingException processing (parsing, generating) JSON content
     */
    public static String object2Json(Object object) throws JsonProcessingException {
        return OBJECTMAPPER.writeValueAsString(object);
    }

    /**
     * json string convert to javaBean
     *
     * @param jsonStr json string
     * @param clazz   javaBean.class
     * @return javaBean
     * @throws IOException if parsing error
     */
    public static <T> T json2Object(String jsonStr, Class<T> clazz) throws IOException {
        return OBJECTMAPPER.readValue(jsonStr, clazz);
    }

    /**
     * json array string convert to list with javaBean
     *
     * @param jsonArrayStr json array string
     * @param clazz        javaBean class
     * @param <T>          javaBean
     * @return a list with javaBean
     * @throws IOException if parsing error
     */
    public static <T> List<T> json2List(String jsonArrayStr, Class<T> clazz) throws IOException {
        List<Map<String, Object>> list = OBJECTMAPPER.readValue(jsonArrayStr, new TypeReference<List<T>>() {
        });
        List<T> result = new ArrayList<>();
        for (Map<String, Object> map : list) {
            result.add(OBJECTMAPPER.convertValue(map, clazz));
        }
        return result;
    }


    /**
     * json string convert to map key must is string : value is javaBean
     *
     * @param jsonStr the key is must string type
     * @param clazz   javaBean type
     * @param <T>
     * @return Map<String ,   T>
     * @throws IOException if parsing error
     */
    public static <T> Map<String, T> json2Map(String jsonStr, Class<T> clazz) throws IOException {
        Map<String, Map<String, Object>> map = OBJECTMAPPER.readValue(jsonStr, new TypeReference<Map<String, T>>() {
        });
        Map<String, T> result = new HashMap<String, T>();
        for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
            result.put(entry.getKey(), OBJECTMAPPER.convertValue(entry.getValue(), clazz));
        }
        return result;
    }
}
